//销售员第三层
const { salesmansModel } = require('./models/salesmansModel')
const mongoose = require("mongoose");
const { usersModel } = require('./models/usersModel');
const { ordersModel } = require("./models/ordersModels");
//销售员登录
module.exports.salesmansLogin = async a => {
    const data = await salesmansModel.find(a)
    return data
}
//新增销售员
module.exports.addSalesmans = async (params) => {
    return await salesmansModel.create(params);
}

// 获取销售员数据
module.exports.getSalesmans = async (params) => {
    return await salesmansModel.find(params)
}
// 删除销售员
module.exports.deleteSalesmans = async (params) => {
    return await salesmansModel.deleteOne(params)
}
// 获取全部订单
module.exports.getSellersOrderById = async function getSellersOrderById({ _id, pageSize,
    currentPage, nameData, timeData }) {
        console.log( _id, pageSize,
            currentPage, nameData, timeData )
    const usersId = (await usersModel.find({
        sellersId: _id, userName: {
            $regex: nameData,
            $options: "$i"
        }
    }, '_id')).map(item => item._id);

    const allOrders = await ordersModel.find({
        usersId: { $in: usersId }, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })
        .populate({
            path: "insuresId",
            populate: {
                path: "typesId",
            },
        })
        .populate('usersId')
        .limit(pageSize - 0).skip((currentPage - 1) * pageSize);


    const paidOrders = await ordersModel.find({
        usersId: { $in: usersId }, ordersPayState: 0, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })
        .populate({
            path: "insuresId",
            populate: {
                path: "typesId",
            },
        })
        .populate('usersId')
        .limit(pageSize - 0).skip((currentPage - 1) * pageSize);

    const payOffOrders = await ordersModel.find({
        usersId: { $in: usersId }, ordersPayState: 1, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })
        .populate({
            path: "insuresId",
            populate: {
                path: "typesId",
            },
        })
        .populate('usersId')
        .limit(pageSize - 0).skip((currentPage - 1) * pageSize);

    const toCancelOrders = await ordersModel.find({
        usersId: { $in: usersId }, ordersPayState: 4, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })
        .populate({
            path: "insuresId",
            populate: {
                path: "typesId",
            },
        })
        .populate('usersId')
        .limit(pageSize - 0).skip((currentPage - 1) * pageSize);



    const toCancelingOrders = await ordersModel.find({
        usersId: { $in: usersId }, ordersPayState: 2, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })
        .populate({
            path: "insuresId",
            populate: {
                path: "typesId",
            },
        })
        .populate('usersId')
        .limit(pageSize - 0).skip((currentPage - 1) * pageSize);

    const toCanceledOrders = await ordersModel.find({
        usersId: { $in: usersId }, ordersPayState: 3, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })
        .populate({
            path: "insuresId",
            populate: {
                path: "typesId",
            },
        })
        .populate('usersId')
        .limit(pageSize - 0).skip((currentPage - 1) * pageSize);

    const allTotal = await ordersModel.countDocuments({
        usersId: { $in: usersId }, delete: false, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })
    const paidTotal = await ordersModel.countDocuments({
        usersId: { $in: usersId }, delete: false, ordersPayState: 0, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })
    const payOffTotal = await ordersModel.countDocuments({
        usersId: { $in: usersId }, delete: false, ordersPayState: 1, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })

    const toCancelingTotal = await ordersModel.countDocuments({
        usersId: { $in: usersId }, delete: false, ordersPayState: 2, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })
    const toCanceledTotal = await ordersModel.countDocuments({
        usersId: { $in: usersId }, delete: false, ordersPayState: 3, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })

    const toCancelTotal = await ordersModel.countDocuments({
        usersId: { $in: usersId }, delete: false, ordersPayState: 4, ordersTime: {
            $regex: timeData,
            $options: "$i"
        }
    })
    console.log(toCancelOrders);
    // const pages = Math.ceil(total / pageSize)
    // const ordersId = (await ordersModel.find({ usersId: { $in: usersId } })).map(item => item._id);
    return {
        allOrders,
        paidOrders,
        payOffOrders,
        toCancelingOrders,
        toCanceledOrders,
        toCancelOrders,
        allTotal,
        paidTotal,
        payOffTotal,
        toCancelingTotal,
        toCanceledTotal,
        toCancelTotal
    }
}

//修改销售员
module.exports.updateSalesmans = async (params) => {
    return await salesmansModel.updateOne({ _id: params._id }, params)
}
// module.exports.updateSalesmans = async (params) => {
//     return await salesmansModel.updateOne({ _id: params._id }, params)
// }
